package indexer;

import java.net.HttpURLConnection;
import java.net.URI;
import java.util.List;

public abstract class Document {
	protected URI _Uri;
	private String _MimeType = "";

	protected List<String> _localLinks;

	public abstract boolean GetResponse(HttpURLConnection webresponse);

	public abstract void Parse();

	// public delegate void
	// DocumentProgressEventHandler(Report.ProgressEventArgs pea);
	// public event DocumentProgressEventHandler DocumentEvent;

	// protected void DocumentProgressEvent(Report.ProgressEventArgs pea)
	// {
	// if (this.DocumentEvent != null)
	// {
	// DocumentEvent(pea);
	// }
	// }

	public URI getUri() {
		return _Uri;
	}

	public void setUri(URI _Uri) {
		this._Uri = _Uri;
	}

	public String getMimeType() {
		return _MimeType;
	}

	public void setMimeType(String _MimeType) {
		this._MimeType = _MimeType;
	}

	public List<String> getLocalLinks() {
		return _localLinks;
	}

	public void setLocalLinks(List<String> _localLinks) {
		this._localLinks = _localLinks;
	}

	// / <summary>
	// / Constructor for any document requires the Uri be specified
	// / </summary>
	public Document(URI uri) {
		_Uri = uri;
	}

	// / <summary>
	// / Constructor for any document requires the Uri and MimeType to be
	// specified
	// / </summary>
	public Document(URI uri, String mimeType) {
		_Uri = uri;
		_MimeType = mimeType;
	}

	// / <summary>
	// / Is the value of the href pointing to a web page?
	// / </summary>
	// / <param name="foundHref">The value of the href that needs to be
	// interogated.</param>
	// / <returns>Boolen </returns>
	public static boolean IsAWebPage(String foundHref) {
		if (foundHref.length() < 2 || foundHref.indexOf("javascript:") > -1
				|| foundHref.indexOf("mailto:") > -1
				|| foundHref.startsWith("#") || foundHref.startsWith("file://")
				|| foundHref.startsWith("\\\\")
				|| foundHref.startsWith("ftp://")) {
			return false;
		}
		
//		if (!URI.IsWellFormedUriString(foundHref, UriKind.RelativeOrAbsolute)) {
//			return false;
//		}

		String extension = "";

		try {
			URI uri = new URI(foundHref);
			if (uri.Segments.Length == 0) {
			}
			String lastSegment = uri.Segments[uri.Segments.Length - 1];

			if (lastSegment.contains(".")) {
				extension = lastSegment.substring(
						lastSegment.lastIndexOf(".") + 1, lastSegment.length()
								- lastSegment.lastIndexOf(".") - 1);
			} else {
				return true;
			}
		} catch (Exception e)// relative url
		{
			if (foundHref.contains(".")) {
				extension = foundHref.substring(foundHref.lastIndexOf(".") + 1,
						foundHref.length() - foundHref.lastIndexOf(".") - 1);
			} else {
				return true;
			}
		}

		if (extension.contains("htm") || extension.contains("asp")
				|| extension == "cgi" || extension == "php"
				|| extension == "jsf" || extension == "jsp"
				|| extension == "pl"
				|| // image extensions
				extension == "jpg" || extension == "jpeg" || extension == "bmp"
				|| extension == "png" || extension == "gif") {
			return true;
		}

		return false;
	}

	/**
	 * False if the link exists, so the website must be skipped
	 * 
	 * @param uri
	 * @return
	 */
	protected boolean AddURLtoGlobalVisited(URI uri) {
		synchronized (Spider.GlobalVisitedURLs) {
			String link = Common.GetHttpAuthority(uri);
			if (!Spider.GlobalVisitedURLs.contains(link)) {
				Spider.GlobalVisitedURLs.add(link);

				// DocumentProgressEvent(new
				// Report.ProgressEventArgs(Report.EventTypes.Start,
				// "Crawling website(redirected):" + link));
				return true;
			} else {
				return false;
			}
		}
	}
}
